package com.yuan.java.java.io;

import org.junit.Test;

import java.io.*;

/**
 * @author yuaneg 2019/1/2
 */
public class IOTestDemo {

    /**
     * byte stream 操作
     */
    @Test
    public void byteArray() throws IOException {
        //正常用法
        String string = "this is byte array test";
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(string.getBytes());
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int result = -1;
        while ((result = byteArrayInputStream.read()) != -1) {
            byteArrayOutputStream.write(result);
        }
        //byteArrayOutputStream.flush(); 此方法在这个类中没有被重写  调用不起任何作用
        System.out.println(new String(byteArrayOutputStream.toByteArray()));
        ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
        //copy outputstream
        byteArrayOutputStream.writeTo(byteArrayOutputStream2);
        System.out.println(new String(byteArrayOutputStream2.toByteArray()));
    }

    /**
     * 文件操作
     */
    @Test
    public void file() throws IOException {
        //3个构造方法 第一个和第二个 类似
        // 第三个是文件描述
        FileInputStream fileInputStream = new FileInputStream(IOTestDemo.class.getResource("/demo/pomdemo.xml").getPath());
        //或者直接获取 inputStream = new FileInputStream(IOTestDemo.class.getResourceAsStream("/demo/pomdemo.xml").getPath());
        System.out.println(StreamUtil.toString(fileInputStream));
        //FileDescriptor
        FileOutputStream fileOutputStream = new FileOutputStream(FileDescriptor.err);
        fileOutputStream.write("this is test error".getBytes());
        //像 文件写入数据
        File file = new File("1.txt");
        fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write("this is test".getBytes());
    }


    /**
     * 序列化
     */
    @Test
    public void objectstream() throws Exception {
        File file = new File("D:/user.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        //对象不能用 内部类
        objectOutputStream.writeObject(new User("酒香逢", "123"));
        FileInputStream fileInputStream = new FileInputStream(file);
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        User user = (User) objectInputStream.readObject();
        System.out.println(user);

        //写入到 ByteArrayStream
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream);
        //对象不能用 内部类
        outputStream.writeObject(new User("bytearray", "你好"));
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream);
        User user2 = (User) inputStream.readObject();
        System.out.println(user2);
    }


    /**
     * 组装 io
     */
    @Test
    public void sequence() throws IOException {
        String string = "this is byte array test";
        ByteArrayInputStream inputStream1 = new ByteArrayInputStream(string.getBytes());
        String string2 = "this 222222222222";
        ByteArrayInputStream inputStream2 = new ByteArrayInputStream(string2.getBytes());
        SequenceInputStream sequenceInputStream = new SequenceInputStream(inputStream1, inputStream2);
        System.out.println(StreamUtil.toString(sequenceInputStream));
    }


    @Test
    public void reader2stream() throws IOException {
        String string = "this is byte array test";
        CharArrayReader charArrayReader = new CharArrayReader(string.toCharArray());
        CharArrayWriter charArrayWriter = new CharArrayWriter();
        int result = -1;
        while ((result = charArrayReader.read()) != -1) {
            charArrayWriter.write(result);
        }
        System.out.println(new String(charArrayWriter.toCharArray()));
    }


}
